using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;

namespace MainGame.Menu
{

  public interface ICreditsMenu
  {
    event MenuEvent OnCBackClicked;
  }

  public class CreditsMenu : BaseMenu, ICreditsMenu
  {

    Button creditsBackBtn;
    Vector2 savedPos;
    public bool Animated = false;
    public int Direction = 1;

    public CreditsMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = game.Content.Load<Texture2D>(@"Images//Menu//CreditsMenu");
      bounds = new Rectangle(260, 80, 200, 300);
      game.Services.AddService(typeof(ICreditsMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(ICreditsMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      savedPos.X = bounds.X;
      // Load all buttons
      

      creditsBackBtn = new Button(Game, @"Images//Menu//button_play", new Rectangle(600, 400, 140, 45), "large_font", "Back");
      AddButton(creditsBackBtn, () =>
      {
        Animated = true;
      });

      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {
      
      if (Animated)
      {
        bounds.X += Direction * 15;
       
        foreach (Button b in (from cb in buttons select cb.b))
        {
          if (!b.message.Equals("Back"))
            b.bounds.X += Direction * 15;
        }

        if (bounds.X > (savedPos.X + bounds.Width + 30))
        {
          Direction = -1;
          if (menuDepth == MenuDepths.BackMenu)
          {
            menuDepth = MenuDepths.ForeGround;
            DrawOrder = (int)menuDepth;
          }
          else
          {
            menuDepth = MenuDepths.BackMenu;
            DrawOrder = (int)menuDepth;
          }
        }

        if (bounds.X < savedPos.X)
        {
          int offset = (int)savedPos.X - bounds.X;
          Animated = false;
          Direction = 1;
          bounds.X = (int)savedPos.X;

          foreach (Button b in (from cb in buttons select cb.b))
          {
            if (!b.message.Equals("Back"))
              b.bounds.X += Direction * offset;
          }

          if (menuDepth == MenuDepths.BackMenu)
          {
            if (OnCBackClicked != null)
              OnCBackClicked();
          }

        }

      }


      base.Update(gameTime);
    }

    public event MenuEvent OnCBackClicked;
  }
}
